//
//  Edit_PassWordViewController.m
//  fxxiaoke
//
//  Created by 鞠超 on 16/9/7.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "Edit_PassWordViewController.h"

#define NSStringFromFormat(fmt,...)     [NSString stringWithFormat:fmt,##__VA_ARGS__]

@interface Edit_PassWordViewController () <UIScrollViewDelegate, UITextFieldDelegate>

@property (nonatomic, strong) UIScrollView * scrollView;

//
@property (nonatomic, strong) UITextField * text_OriginalPassword;
@property (nonatomic, strong) UITextField * text_NewPassword;
@property (nonatomic, strong) UITextField * text_NewPassword_1;

@end

@implementation Edit_PassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    
    _lblTitle.text = @"修改密码";
    
    //左返回
    UIImageView * image_LeftRetrun = [[UIImageView alloc] initWithFrame:CGRectMake(11, 31.5, 25, 25)];
    image_LeftRetrun.userInteractionEnabled = YES;
    image_LeftRetrun.image = [UIImage imageNamed:@"Left-return.png"];
    [_topView addSubview:image_LeftRetrun];
    
    UILabel * lbl_return = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_LeftRetrun.frame) - 6, 35, 70, 15)];
    lbl_return.text = @"编辑资料";
    lbl_return.textColor = [UIColor whiteColor];
    lbl_return.font = [UIFont systemFontOfSize:17];
    [_topView addSubview:lbl_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 22, 90, 40);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 60);
    self.scrollView.delegate = self;
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    //手势
    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ScrollViewAction:)];
    tapGesture.numberOfTapsRequired = 1; //点击次数
    tapGesture.numberOfTouchesRequired = 1; //点击手指数
    [self.scrollView addGestureRecognizer:tapGesture];
    
    //输入原始密码
    UIView * view_OriginalPassWord = [[UIView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, 50)];
    view_OriginalPassWord.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:view_OriginalPassWord];
    
    self.text_OriginalPassword = [[UITextField alloc] initWithFrame:CGRectMake(15, 12.5, SCREEN_WIDTH - 15 - 50, 25)];
    self.text_OriginalPassword.placeholder = @"输入原始密码";
    self.text_OriginalPassword.delegate = self;
    self.text_OriginalPassword.font = [UIFont systemFontOfSize:17.0f];
    self.text_OriginalPassword.secureTextEntry = YES;
    self.text_OriginalPassword.returnKeyType = UIReturnKeyDone;
    [view_OriginalPassWord addSubview:self.text_OriginalPassword];
    
    UIView * view_line1 = [[UIView alloc] initWithFrame:CGRectMake(12, 49, SCREEN_WIDTH - 24, 1)];
    view_line1.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [view_OriginalPassWord addSubview:view_line1];
    
    UIButton * btn_1 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_1.frame = CGRectMake(SCREEN_WIDTH - 40, 10, 30, 30);
    [btn_1 setBackgroundImage:[UIImage imageNamed:@"unlook@2x.png"] forState:(UIControlStateNormal)];
    [view_OriginalPassWord addSubview:btn_1];
    [btn_1 addTarget:self action:@selector(btn_1Action:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    //输入密码
    UIView * view_NewPassword = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(view_OriginalPassWord.frame), SCREEN_WIDTH, 50)];
    view_NewPassword.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:view_NewPassword];
    
    self.text_NewPassword = [[UITextField alloc] initWithFrame:CGRectMake(15, 12.5, SCREEN_WIDTH - 15 - 50, 25)];
    self.text_NewPassword.placeholder = @"输入密码";
    self.text_NewPassword.delegate = self;
    self.text_NewPassword.font = [UIFont systemFontOfSize:17.0f];
    self.text_NewPassword.secureTextEntry = YES;
    self.text_NewPassword.returnKeyType = UIReturnKeyDone;
    [view_NewPassword addSubview:self.text_NewPassword];
    
    UIView * view_line2 = [[UIView alloc] initWithFrame:CGRectMake(12, 49, SCREEN_WIDTH - 24, 1)];
    view_line2.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [view_NewPassword addSubview:view_line2];
    
    UIButton * btn_2 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_2.frame = CGRectMake(SCREEN_WIDTH - 40, 10, 30, 30);
    [btn_2 setBackgroundImage:[UIImage imageNamed:@"unlook@2x.png"] forState:(UIControlStateNormal)];
    [view_NewPassword addSubview:btn_2];
    [btn_2 addTarget:self action:@selector(btn_2Action:) forControlEvents:(UIControlEventTouchUpInside)];

    
    //再次输入密码
    UIView * view_NewPassword_1 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(view_NewPassword.frame), SCREEN_WIDTH, 50)];
    view_NewPassword_1.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:view_NewPassword_1];
    
    self.text_NewPassword_1 = [[UITextField alloc] initWithFrame:CGRectMake(15, 12.5, SCREEN_WIDTH - 15 - 50, 25)];
    self.text_NewPassword_1.placeholder = @"请再次输入密码";
    self.text_NewPassword_1.delegate = self;
    self.text_NewPassword_1.font = [UIFont systemFontOfSize:17.0f];
    self.text_NewPassword_1.secureTextEntry = YES;
    self.text_NewPassword_1.returnKeyType = UIReturnKeyDone;
    [view_NewPassword_1 addSubview:self.text_NewPassword_1];
    
    UIView * view_line3 = [[UIView alloc] initWithFrame:CGRectMake(12, 49, SCREEN_WIDTH - 24, 1)];
    view_line3.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [view_NewPassword_1 addSubview:view_line3];
    
    UIButton * btn_3 = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_3.frame = CGRectMake(SCREEN_WIDTH - 40, 10, 30, 30);
    [btn_3 setBackgroundImage:[UIImage imageNamed:@"unlook@2x.png"] forState:(UIControlStateNormal)];
    [view_NewPassword_1 addSubview:btn_3];
    [btn_3 addTarget:self action:@selector(btn_3Action:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //
    UILabel * lbl_Prompt = [[UILabel alloc] initWithFrame:CGRectMake(13, CGRectGetMaxY(view_NewPassword_1.frame) + 20, SCREEN_WIDTH - 26, 15)];
    lbl_Prompt.text = @"密码须为6位及以上，数字和字母的组合";
    lbl_Prompt.font = [UIFont systemFontOfSize:15];
    lbl_Prompt.textColor = [UIColor grayColor];
    [self.scrollView addSubview:lbl_Prompt];
    
    //保存 250 176 96
    UIButton * btn_Save = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_Save.frame = CGRectMake(12, CGRectGetMaxY(lbl_Prompt.frame) + 35, SCREEN_WIDTH - 24, 45);
    btn_Save.layer.cornerRadius = 5;
    btn_Save.backgroundColor = [UIColor colorWithRed:250/255.0 green:176/255.0 blue:96/255.0 alpha:1.0f];
    [btn_Save setTitle:@"保存" forState:(UIControlStateNormal)];
    [btn_Save setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    btn_Save.titleLabel.font = [UIFont systemFontOfSize:18.0f];
    [self.scrollView addSubview:btn_Save];
    [btn_Save addTarget:self action:@selector(btn_SaveAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //电话
    UILabel * tel_detail = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2 - 90, CGRectGetMaxY(btn_Save.frame) + 20, 180, 15)];
    tel_detail.textAlignment = NSTextAlignmentCenter;
    tel_detail.font = [UIFont systemFontOfSize:13];
    [self.scrollView addSubview:tel_detail];
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:NSStringFromFormat(@"有问题？详情 400 888 8888")];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:250/255.0 green:176/255.0 blue:96/255.0 alpha:1.0f] range:NSMakeRange(6,str.length - 6)];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0,6)];
    tel_detail.attributedText = str;
    
    UIButton * btn_telDetail = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_telDetail.frame = CGRectMake(SCREEN_WIDTH / 2 - 90, CGRectGetMaxY(btn_Save.frame) + 20, 180, 15);
    [self.scrollView addSubview:btn_telDetail];
    [btn_telDetail addTarget:self action:@selector(btn_telDetailAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

#pragma mark - 点击保存按钮
- (void )btn_SaveAction:(UIButton *)sender
{
    NSLog(@"点击保存,掉接口");
    
    [self.text_OriginalPassword resignFirstResponder];
    [self.text_NewPassword resignFirstResponder];
    [self.text_NewPassword_1 resignFirstResponder];
}

#pragma mark - 点击拨打电话
- (void )btn_telDetailAction:(UIButton *)sender
{
//    NSLog(@"点击拨打电话");
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"400-8888-888" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
    [self presentViewController:alert animated:YES completion:^{
        
    }];
    
    UIAlertAction * action = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:action];
    
    UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"呼叫" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        NSURL * url_tel = [NSURL URLWithString:[NSString stringWithFormat:@"tel://400-8888-888"]];
        [[UIApplication sharedApplication] openURL:url_tel];
        
    }];
    [alert addAction:action1];
}

#pragma mark - 原始密码的按钮
- (void )btn_1Action:(UIButton *)sender
{
    if(self.text_OriginalPassword.secureTextEntry == YES)
    {
        NSString * str = self.text_OriginalPassword.text;
        
        self.text_OriginalPassword.secureTextEntry = NO;
        self.text_OriginalPassword.text = @"";
        self.text_OriginalPassword.text = str;
        
        [sender setBackgroundImage:[UIImage imageNamed:@"look@2x.png"] forState:(UIControlStateNormal)];
    }
    else
    {
        NSString * str = self.text_OriginalPassword.text;
        
        self.text_OriginalPassword.secureTextEntry = YES;
        self.text_OriginalPassword.text = @"";
        self.text_OriginalPassword.text = str;
        
        [sender setBackgroundImage:[UIImage imageNamed:@"unlook@2x.png"] forState:(UIControlStateNormal)];
    }
}

#pragma mark - 输入密码的按钮
- (void )btn_2Action:(UIButton *)sender
{
    if(self.text_NewPassword.secureTextEntry == YES)
    {
        NSString * str = self.text_NewPassword.text;
        
        self.text_NewPassword.secureTextEntry = NO;
        self.text_NewPassword.text = @"";
        self.text_NewPassword.text = str;
        
        [sender setBackgroundImage:[UIImage imageNamed:@"look@2x.png"] forState:(UIControlStateNormal)];
    }
    else
    {
        NSString * str = self.text_NewPassword.text;
        
        self.text_NewPassword.secureTextEntry = YES;
        self.text_NewPassword.text = @"";
        self.text_NewPassword.text = str;
        
        [sender setBackgroundImage:[UIImage imageNamed:@"unlook@2x.png"] forState:(UIControlStateNormal)];
    }
}

#pragma mark - 再次输入密码的按钮
- (void )btn_3Action:(UIButton *)sender
{
    if(self.text_NewPassword_1.secureTextEntry == YES)
    {
        NSString * str = self.text_NewPassword_1.text;
        
        self.text_NewPassword_1.secureTextEntry = NO;
        self.text_NewPassword_1.text = @"";
        self.text_NewPassword_1.text = str;
        
        [sender setBackgroundImage:[UIImage imageNamed:@"look@2x.png"] forState:(UIControlStateNormal)];
    }
    else
    {
        NSString * str = self.text_NewPassword_1.text;
        
        self.text_NewPassword_1.secureTextEntry = YES;
        self.text_NewPassword_1.text = @"";
        self.text_NewPassword_1.text = str;
        
        [sender setBackgroundImage:[UIImage imageNamed:@"unlook@2x.png"] forState:(UIControlStateNormal)];
    }
}

#pragma mark - scrollView代理
- (void )scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.text_OriginalPassword resignFirstResponder];
    [self.text_NewPassword resignFirstResponder];
    [self.text_NewPassword_1 resignFirstResponder];
}

#pragma mark - textfield代理
- (BOOL )textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    
    return YES;
}

#pragma mark - scrollView上的手势
- (void )ScrollViewAction:(id )sender
{
//    [self.text_OriginalPassword resignFirstResponder];
//    [self.text_NewPassword resignFirstResponder];
//    [self.text_NewPassword_1 resignFirstResponder];
}


@end
